package com.cdl.library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cdl.library.R;
import com.cdl.library.listener.MoreButtonListener;

public class SettingClickView extends RelativeLayout {

    public SettingClickView(Context context) {
        this(context, null);
    }

    public SettingClickView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    View view;
    TextView tv_title, tv_content;

    String txt_title = "";
    String txt_content = "";
    String btn_name = "";
    LinearLayout lin_click_View;
    private boolean clickEnable = true;

    public SettingClickView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Setting_Button);
        txt_title = a.getString(R.styleable.Setting_Button_txt_title);
        txt_content = a.getString(R.styleable.Setting_Button_txt_content);
        btn_name = a.getString(R.styleable.Setting_Button_btn_txt_right);
        boolean showBottom = a.getBoolean(R.styleable.Setting_Button_show_bottom_line, true);
        boolean showRightIcon = a.getBoolean(R.styleable.Setting_Button_show_right_icon, true);
        view = LayoutInflater.from(context).inflate(R.layout.view_setting_click, null);
        TextView tv_right_icon = (TextView) view.findViewById(R.id.tv_right_icon);
        tv_right_icon.setVisibility(showRightIcon ? View.VISIBLE : View.INVISIBLE);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        lin_click_View = (LinearLayout) view.findViewById(R.id.lin_click_View);
        tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_title.setText(txt_title);
        tv_content.setText(txt_content);
        initListener();
        addView(view);
        a.recycle();
    }

    public void setTxtContent(String getresolution) {
        if (tv_content != null) {
            tv_content.setText(getresolution);
        }
    }

    public void setClickEnable(boolean enable) {
        clickEnable = enable;
        if (clickEnable) {
            tv_title.setTextColor(getContext().getColor(R.color.yancy_grey900));
        } else {
            tv_title.setTextColor(getContext().getColor(R.color.yancy_grey600));
        }
    }

    public String getTextContent() {
        if (tv_content != null) {
            return tv_content.getText().toString();
        }
        return "";
    }

    private void initListener() {
        lin_click_View.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!clickEnable) {
                    return;
                }
                if (listener != null) {
                    listener.clickView(SettingClickView.this);
                }
            }
        });
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            layout(l, t, r, b);
        }
    }

    MoreButtonListener listener;

    public void setOnMoretListener(MoreButtonListener listener) {
        this.listener = listener;
    }


}
